local extension = Package:new("tea_ex_d")
extension.extensionName = "wild_collection"
Fk:loadTranslationTable{
  ["tea_ex_d"] = "茶水闲设四",
}
local U = require "packages/utility/utility"
local Est = require "packages/wild_collection/utility"

local zhangxiu = General(extension, "tea__zhangxiu", "qun", 3)
local getNumber = function(ids)
  if type(ids) == "number" then
    return Fk:getCardById(ids).number
  elseif type(ids) == "table" then
    return table.map(ids, function(id)
      return Fk:getCardById(id).number
    end)
  end
end
local isEqualDifferenceSeries = function(list, difference)
  local dif = difference or 1
  table.sort(list)
  local n
  for _, i in ipairs(list) do
    if not n then
      n = i 
    elseif i ~= n + dif then
      return false
    else
      n = i
    end
  end
  return true
end
local fujie = fk.CreateActiveSkill{
  name = "tea_fujie",
  anim_type = "special",
  prompt = "#tea_fujie-prompt",
  min_card_num = 2,
  min_target_num = 1,
  card_filter = function(self, to_select, selected)
    if #selected == 0 then return true 
    else
      local list = table.clone(selected)
      table.insertIfNeed(list, to_select)
      return isEqualDifferenceSeries(getNumber(list))
    end
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected < #selected_cards then
      return not Self:prohibitUse(Fk:cloneCard("peach")) and Fk:currentRoom():getPlayerById(to_select):isWounded()
    end
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and table.find(Fk:currentRoom().alive_players, function(p)
      return not player:isProhibited(p, Fk:cloneCard("peach"))
    end)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local tos = effect.tos
    local cards = effect.cards
    room:sortPlayersByAction(tos)
    room:recastCard(cards, player, self.name)
    local c = Fk:cloneCard("peach")
    c.skillName = self.name
    local use = {
      from = player.id,
      tos = table.map(tos, function(id)
        return {id}
      end),
      card = c,
    }
    room:useCard(use)
    for _, id in ipairs(tos) do
      local to = room:getPlayerById(id)
      if not player.dead and player.hp >= #cards then
        if player:isNude() then
          room:damage{
            to = to,
            damage = 2,
            damageType = fk.ThunderDamage,
            skillName = self.name,
          }
        else
          local ids = table.filter(to:getCardIds("he"), function(id)
            return table.find(getNumber(cards), function(num)
              return num <= getNumber(id)
            end) and table.find(getNumber(cards), function(num)
              return num >= getNumber(id)
            end)
          end)
          local cnumbers = getNumber(cards)
          table.sort(cnumbers)
          local cid = room:askForCard(to, 1, 1, true, self.name, true, tostring(Exppattern({id = ids})), "#tea_fujie-choose:::" ..
           tostring(cnumbers[1]) .. "~" .. tostring(cnumbers[#cnumbers]))
          if #cid == 1 then
            room:recastCard(cid, to, self.name)
          else
            room:damage{
              to = to,
              damage = 2,
              damageType = fk.ThunderDamage,
              skillName = self.name,
            }
          end
        end
      end
    end
  end,
}
local xuzhong = fk.CreateActiveSkill{
  name = "tea_xuzhong",
  anim_type = "special",
  prompt = "#tea_xuzhong-prompt",
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:getMark("@@tea_xuzhong_mark-turn") == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cids = {}
    for i = 1, 3 do
      table.insert(cids, room.draw_pile[i])
    end
    if #cids > 0 then
      U.viewCards(player, cids, self.name)
      local hands = getNumber(player:getCardIds("h"))
      local cards = table.filter(cids, function(id)
        return not table.contains(hands, getNumber(id)) and 
        table.find(hands, function(num)
          return num <= getNumber(id)
        end) and table.find(hands, function(num)
          return num >= getNumber(id)
        end)
      end)
      if #cards > 0 then
        room:obtainCard(player, cards, false, fk.ReasonPrey)
      else
        room:loseHp(player, 1 ,self.name)
        if not player.dead then
          room:obtainCard(player, cids, false, fk.ReasonPrey)
        end
        room:setPlayerMark(player, "@@tea_xuzhong_mark-turn", 1)
      end
    end
  end,
}
zhangxiu:addSkill(fujie)
zhangxiu:addSkill(xuzhong)
Fk:loadTranslationTable{
  ["tea__zhangxiu"] = "张修",
  ["#tea__zhangxiu"] = "三通天师",
  ["designer:tea__zhangxiu"] = "小叶子",
  ["cv:tea__zhangxiu"] = "茶社",
  ["illustrator:tea__zhangxiu"] = "Gamen",
  ["tea_fujie"] = "符节",
  [":tea_fujie"] = "出牌阶段限一次，你可以重铸至少两张点数连续的牌，视为对至多等量角色使用一张【桃】，然后你令其中体力值不小于X的角色依次选择"..
  "一项（X为你以此法重铸的牌数）：1.重铸一张点数于此区间的牌；2.受到 2 点雷电伤害。",
  ["#tea_fujie-prompt"] = "符节：你可以重铸至少两张点数连续的牌，视为对至多等量角色使用一张【桃】",
  ["#tea_fujie-choose"] = "符节：重铸一张点数位于 %arg 的牌，或点取消并受到2点雷电伤害",
  ["tea_xuzhong"] = "蓄众",
  [":tea_xuzhong"] = "出牌阶段，你可以观看牌堆顶三张牌，然后获得其中点数与你手牌均不同且在点数最大与最小之间的牌。若你未以此法获得牌"..
  "，你失去1点体力并获得这些牌，然后此技能本回合失效。",
  ["#tea_xuzhong-prompt"] = "蓄众：你可以观看牌堆顶三张牌，有机会获得其中的牌",
  ["@@tea_xuzhong_mark-turn"] = "蓄众失效",
}

return extension